﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Keepo.Models;

namespace AlbumDesigner.Helpers
{
    public class AlbumPrintVersionHelper
    {
        static public void RemovePrintVersion(int printVersionId)
        {
            using (var db = new AlbumContext())
            {
                var albumPrintVersion = db.AlbumPrintVersions.Find(printVersionId);

                if (albumPrintVersion != null)
                {
                    if (!string.IsNullOrEmpty(albumPrintVersion.InnerUrl))
                    {
                        var innerFilePath = HttpContext.Current.Server.MapPath("~/" + albumPrintVersion.InnerUrl);
                        //delete the pdf inner file
                        if (System.IO.File.Exists(innerFilePath))
                        {
                            System.IO.File.Delete(innerFilePath);
                        }
                    }

                    if (!string.IsNullOrEmpty(albumPrintVersion.CoverUrl))
                    {
                        var coverFilePath = HttpContext.Current.Server.MapPath("~/" + albumPrintVersion.CoverUrl);
                        //delete the pdf cover file
                        if (System.IO.File.Exists(coverFilePath))
                        {
                            System.IO.File.Delete(coverFilePath);
                        }
                    }

                    // Remove the entity
                    db.AlbumPrintVersions.Remove(albumPrintVersion);
                    db.SaveChanges();
                }
            }
        }

    }
}